package com.huika.renrench.control.my.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.fragment.ColleCtcarveFra;
import com.huika.renrench.control.my.fragment.ColleStoreFra;
import com.huika.renrench.view.TitleBarHelper;

/**
 * @author yangyuan
 * @date：2015年10月22日
 * @describe:我的推荐
 */
public class UserInviteActivity extends BaseAct{

	private LinearLayout lin_suggestion_left;//我的投资
	private TextView text_suggestion_left;
	private ImageView img_suggestion_left;
	
	private LinearLayout lin_suggestion_rigte;//我的预约
	private TextView text_suggestion_rigte;
	private ImageView img_suggestion_rigte;
	
	private int lastId;
	private Fragment lastFragment;
	private FragmentTransaction transaction;
	private FragmentManager fragmentManager;
	private Fragment fragment = null;
	private static final int MENU_1 = 1;
	private static final int MENU_2 = 2;
	
	TitleBarHelper title;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.user_collect_act); 
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		title = new TitleBarHelper(this, R.string.common_index_my, R.string.compile, R.string.user_collect);
		title.setOnRightClickListener(this);
		
		lin_suggestion_left = (LinearLayout) findViewById(R.id.lin_suggestion_left);
		text_suggestion_left = (TextView) findViewById(R.id.text_suggestion_left);
		img_suggestion_left = (ImageView) findViewById(R.id.img_suggestion_left);
		lin_suggestion_rigte = (LinearLayout) findViewById(R.id.lin_suggestion_rigte);
		text_suggestion_rigte = (TextView) findViewById(R.id.text_suggestion_rigte);
		img_suggestion_rigte = (ImageView) findViewById(R.id.img_suggestion_rigte);
		
		setOclicklisen();
		lin_suggestion_left.performClick();
	}
	
	private void setOclicklisen() {
		lin_suggestion_left.setOnClickListener(this);
		lin_suggestion_rigte.setOnClickListener(this);
	}
	
	@Override
	public void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.lin_suggestion_left:
			cleraChooseLayout(0);
			showTFragment(MENU_1);
			break;
		case R.id.lin_suggestion_rigte:
			cleraChooseLayout(1);
			showTFragment(MENU_2);
			break;
		case R.id.right:
			//showToastMsg("邀请");
			showActivity(this, ToInviteActivity.class);
			break;
		}
	}
	
	//更新UI
	private void cleraChooseLayout(int type) {
		if (type==0) {//左
			text_suggestion_left.setTextColor(getResources().getColor(R.color.color_3698e5));
			text_suggestion_rigte.setTextColor(getResources().getColor(R.color.color_666666));
			img_suggestion_left.setVisibility(View.VISIBLE);
			img_suggestion_rigte.setVisibility(View.INVISIBLE);
		}else {
			text_suggestion_rigte.setTextColor(getResources().getColor(R.color.color_3698e5));
			text_suggestion_left.setTextColor(getResources().getColor(R.color.color_666666));
			img_suggestion_rigte.setVisibility(View.VISIBLE);
			img_suggestion_left.setVisibility(View.INVISIBLE);
		}
	}
	
	private void showTFragment(int id) {
		String tag = id + "";
		if (fragmentManager == null) { 
			fragmentManager = getSupportFragmentManager();
		}
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		transaction = fragmentManager.beginTransaction();
		if (fragment == null) {
			fragment = getFragmentByTag(id);
			transaction.add(R.id.lin_suggestion, fragment, tag);
		} else {
			transaction.show(fragment);
		}
		if (lastFragment != null && lastFragment != fragment) {
			transaction.hide(lastFragment);
		}
		lastFragment = fragment;
		transaction.commitAllowingStateLoss();
		lastId = id;
	}

	private Fragment getFragmentByTag(int tag) {
		switch (tag) {
			case MENU_1:
				ColleCtcarveFra firament = new ColleCtcarveFra();
				fragment = firament;
				break;
			case MENU_2: 
				ColleStoreFra firament1 = new ColleStoreFra();
				fragment = firament1;
				break;
		}
		return fragment;
	}

}
